package operation;
import Book.BookList;
import Book.Book;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 洪汤泽大帅哥
 * Date: 2024-11-02
 * Time: 10:59
 */
public class BorrowBook implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书....");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想要借阅的图书：");
        String name = sc.nextLine();
        //1.先遍历数组 查找是否存在要借阅的图书
        int currSize = bookList.getUsedSize();
        for (int i = 0; i < currSize; i++) {
            Book book = bookList.getBooks(i);
            //2.如果存在，检查是否已经被借出
            if(book.getName().equals(name)){
                if (book.isBorrowed()){
                    System.out.println("这本书已经被借出了！！！");
                }
                //4.如果没有被借出 可以借 否则不能借
                else {
                    System.out.println("您成功借出该书！！！");
                    book.setBorrowed(true);
                    System.out.println(book);
                }
                return;
            }
        }
        //4.如果不存在 则不能借阅
        System.out.println("这本书不存在，不能借阅！！！");
    }
}
